package com.example.demo;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.types.RedisClientInfo;

public class RedisT extends DemoApplicationTests {

	@Autowired
	private RedisTemplate redisTemplate;

	@Test
	public void testSingle() {
		redisTemplate.opsForValue().set("name", "scott");
		System.out.println(redisTemplate.opsForValue().get("name"));
	}

	@Test
	public void test() throws Exception {
		int count = 500;

		CountDownLatch countDownLatch = new CountDownLatch(count);
		for (int i = 0; i < count; i++) {
			new Executor1(countDownLatch, redisTemplate).start();
		}
		Thread.sleep(50000);
	}

}

class Executor1 extends Thread {

	private final CountDownLatch countDownLatch;
	private final RedisTemplate redisTemplate;

	public Executor1(CountDownLatch countDownLatch, RedisTemplate redisTemplate) {
		this.countDownLatch = countDownLatch;
		this.redisTemplate = redisTemplate;
	}

	@Override
	public void run() {
		try {
			countDownLatch.countDown();
			System.out.println("count is " + countDownLatch.getCount());
			Random random = new Random();
			countDownLatch.await();
			ValueOperations operation = redisTemplate.opsForValue();
			for (int i = 0; i < 5; i++) {
				System.out.println("--------start" + i + "-----" + System.currentTimeMillis());
				operation.set(System.currentTimeMillis() + i, "value" + i);
			}
		} catch (Exception e) {
		}
	}

}
